package com.alg.test;

import java.util.Scanner;

/**
 * @author Li
 * @Date 2023/4/18 16:48
 */
public class BinarySearch {
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9,10};
        int num = Myscanner("1-10");
        int index = getIndex(array,num);
        System.out.println(index);
    }

    private static int Myscanner(String str) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个"+str+"的整数");
        return scanner.nextInt();
    }

    private static int getIndex(int[] array, int num) {
        int min = 0;
        int max = array.length-1;
        while (min <= max){
            int mid = (min+max)/2;
            if (num == array[mid]){
                return mid;
            }else if (num < array[mid]){
                max = mid-1;
            }else if (num > array[mid]){
                min = mid+1;
            }

        }
        return -1;
    }
}
